//
//  WTComposeToolbar.swift
//  WTSinaWeibo
//
//  Created by  Wellim on 16/7/3.
//  Copyright © 2016年  Wellim. All rights reserved.
//

import UIKit
// toolbar 按钮枚举
enum WTComposeToolbarType: Int {
     // 图片
    case Picture = 1
    // @
    case Mention = 2
    // #
    case Trend = 3
    //表情
    case Emoticon = 4
    // +
    case Add = 5


}

class WTComposeToolbar: UIView {
    // 供外界设置
    /*
     如果isEmoticon == true 代表显示表情键盘    button显示image 显示键盘
     如果isEmoticon == false 代表显示的系统键盘 button显示image 显示 笑脸
     */
    var emoticonBtn: UIButton?
    
    
    var isEmotion:Bool = false{
    
        didSet{
            if isEmotion {
             // 显示键盘
               emoticonBtn?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                emoticonBtn?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }else{
            // 显示笑脸
               emoticonBtn?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
               emoticonBtn?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"),forState: UIControlState.Highlighted)
            }
        
        }
    
    
    }
    

  // 告知外界按钮点击啦
    var closure:((type: WTComposeToolbarType) ->())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
  
    // MARK: - 设置视图
    
    private func setUpUI(){
        
        backgroundColor = UIColor.whiteColor()
    // 添加控件
   let pictureBtn = addChildButtons("compose_toolbar_picture",type: .Picture)
   let mentionBtn = addChildButtons("compose_mentionbutton_background",type: .Mention)
   let trendBtn = addChildButtons("compose_trendbutton_background",type: .Trend)
    emoticonBtn = addChildButtons("compose_emoticonbutton_background",type: .Emoticon)
   let addBtn = addChildButtons("compose_add_background",type: .Add)
   // 添加约束
      pictureBtn.snp_makeConstraints { (make) in
         make.left.top.bottom.equalTo(self)
         make.width.equalTo(mentionBtn)
        
        }
      
        mentionBtn.snp_makeConstraints { (make) in
          make.top.bottom.equalTo(self)
          make.left.equalTo(pictureBtn.snp_right)
          make.width.equalTo(trendBtn)
        }
     
        trendBtn.snp_makeConstraints { (make) in
          make.top.bottom.equalTo(self)
          make.left.equalTo(mentionBtn.snp_right)
          make.width.equalTo(emoticonBtn!)
        }
        
        emoticonBtn!.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(trendBtn.snp_right)
            make.width.equalTo(addBtn)
        }
        
        addBtn.snp_makeConstraints { (make) in
            
          make.top.right.bottom.equalTo(self)
          make.left.equalTo(emoticonBtn!.snp_right)
            
        }
    
    }
    
     // 创建button按钮的公共方法
    
    func addChildButtons(imageName: String,type:WTComposeToolbarType) -> UIButton {
       
        let button = UIButton()
         // rawValue 枚举值.rawValue ==  Int
        button.tag = type.rawValue
        // 添加点击事件
        button.addTarget(self, action: "bttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
    
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        addSubview(button)
        return button
    }
    
     // MARK: - 监听方法
    func bttonClick(button:UIButton){
    
        closure?(type:WTComposeToolbarType(rawValue: button.tag)!)
    }
    
    
    

}
